package cn.com.joco.dk.webby.action.user;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.github.miemiedev.mybatis.paginator.domain.PageList;

import cn.com.joco.cf.core.cui.dataTable.DataTableRequest;
import cn.com.joco.cf.core.cui.dataTable.DataTableResponse;
import cn.com.joco.dk.core.common.StatusBean;
import cn.com.joco.dk.webby.model.User;
import cn.com.joco.dk.webby.service.user.UserService;

@Controller
@RequestMapping("/user")
public class UserAction {
	
	@Autowired
	private UserService userService;
	
	@RequestMapping("/list")
	public String userList( Model model) {
		return "user/userlist";
	}
	@RequestMapping("/id")
	public String userId( Model model) {
		return "user/userview";
	}
	@RequestMapping("/list/databale")
	@ResponseBody
	public DataTableResponse<User> userListPage(HttpServletRequest request,
			@RequestParam(value="name",defaultValue="", required = false) String name,
			@RequestParam(value="status",defaultValue="", required = false) int status
			){
		DataTableRequest dataTableRequest = DataTableRequest.buildDataTableRequest(request);
		Page<User> resultPage = userService.findByPage(dataTableRequest.buildPageable(), name, status);
		
		return DataTableResponse.build(resultPage, dataTableRequest);
	}
	
	@RequestMapping("/add")
	public String addUser(Model model) {
		return "user/usersave";
	}
	
	@RequestMapping("/user_add")
	@ResponseBody
	public StatusBean useradd(User user) {
		user.setRegisterDate(new Date());
		StatusBean statusBean = new StatusBean();
		statusBean.setData(statusBean.STATUS_OK);
		return statusBean;
	}
	@RequestMapping("/edit/{id}")
	public ModelAndView userEdit(@PathVariable(value="id")long id) {
		ModelAndView model = new ModelAndView();
		User user = userService.findById(id);
		model.addObject("user", user);
		model.setViewName("user/useredit");
		return model;
	}
}
